package com.tongbo.mobilesafe.ui;

import com.tongbo.mobilesafe.R;
import com.tongbo.mobilesafe.receiver.MyAdmin;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SetupGuide4Activity extends Activity implements OnClickListener {
	private Button bt_pre;
	private Button bt_setup_finish;
	private CheckBox cb_isprotecting;
	private SharedPreferences sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setupguide4);
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		
		bt_pre = (Button) this.findViewById(R.id.bt_previous);
		bt_setup_finish = (Button) this.findViewById(R.id.bt_setup_finish);
		cb_isprotecting = (CheckBox) this.findViewById(R.id.cb_isprotecting);
		//初始化checkbox状态
		boolean isprotecting = sp.getBoolean("isprotecting", false);
		if(isprotecting){
			cb_isprotecting.setText("手机防盗保护中");
			cb_isprotecting.setChecked(true);
		}
		cb_isprotecting.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					cb_isprotecting.setText("手机防盗保护中");
					cb_isprotecting.setChecked(true);
					Editor editor = sp.edit();
					editor.putBoolean("isprotecting", true);
					editor.commit();
				}else{
					cb_isprotecting.setText("手机没有开启防盗能保护");
					cb_isprotecting.setChecked(false);
					Editor editor = sp.edit();
					editor.putBoolean("isprotecting", false);
					editor.commit();
				}
			}
		});
		bt_pre.setOnClickListener(this);
		bt_setup_finish.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_previous:
			Intent intent3 = new Intent(this, SetupGuide3Activity.class);
			//设置完成之后，一定要把当前的activity从当前任务栈中移除
			finish();
			startActivity(intent3);
			//设置activity切换之后的动画效果
			overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
			break;
		case R.id.bt_setup_finish:
			//设置一个标识，标识用户已经完成了手机防护的设置向导
			if(cb_isprotecting.isChecked()){
				finish();
				finishSetup();
			}else{
				AlertDialog.Builder builder = new Builder(SetupGuide4Activity.this);
				builder.setTitle("提醒");
				builder.setMessage("强烈建议开启手机防盗");
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				builder.create().show();
				return;
			}
			break;
		}
	}
	private void finishSetup() {
		Editor editor = sp.edit();
		editor.putBoolean("issetupalready", true);
		editor.commit();
		DevicePolicyManager manager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
		ComponentName mAdminName = new ComponentName(this, MyAdmin.class);
		if (!manager.isAdminActive(mAdminName)) {
			Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
			intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
			startActivity(intent);
		}
	}
}